package com.example.controller.seller;

import com.example.base.BaseController;
import com.example.entity.*;
import com.example.service.*;
import com.example.util.BeanUtils;
import com.example.vo.Result;
import jakarta.annotation.Resource;
import jdk.jfr.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lsc
 * @date 2024/6/9
 */
@RestController()
@Slf4j
@RequestMapping("/seller/spu")
public class SellerSpuController extends BaseController<SpuService,Spu> {
    @Resource
    private SpuAttrValueService spuAttrValueService;

    @Resource
    private SkuService skuService;

    @Resource
    private SkuAttrValueService skuAttrValueService;

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Resource
    private AdminSpuExamService adminSpuExamService;

    @Resource
    private AdminSpuAttrValueExamService adminSpuAttrValueExamService;

    @Resource
    private AdminSkuExamService adminSkuExamService;

    @Resource
    private StoreService storeService;

    @Resource
    private AdminSkuAttrValueExamService adminSkuAttrValueExamService;

    @Resource
    private AdminSkuImagesExamService adminSkuImagesExamService;

    @GetMapping("/getSpuList")
    public Result<List<SpuVo>> getSpuList(@RequestParam("storeId") Long storeId){
        List<Spu> spu = baseService.getStoreSpuList(storeId);

        List<SpuVo> spuVo = BeanUtils.copyList(spu,SpuVo.class);

        for (SpuVo vo : spuVo) {
            Integer spuId = vo.getId().intValue();

            List<SpuAttrValue> spuAttrValueList = spuAttrValueService.getSpuAttrsBySpuId(spuId);

            vo.setBaseAttrs(spuAttrValueList);

            List<SkuVo> skus = skuService.getSkusBySpuId(spuId);

            List<Long> skuIds = new ArrayList<>();

            for (SkuVo skuVo : skus) {
                Long skuId = skuVo.getId();

                skuIds.add(skuId);

                skuVo.setSaleAttrs(skuAttrValueService.getSkuAttrsBySkuId(skuId));

                skuVo.setImages(skuService.getSkuImagesById(skuId));
            }

            vo.setSkus(skus);

            vo.setAllSaleAttrs(skuService.getAllSkuSaleAttrBySkuIds(skuIds));

            vo.setSpuImages(skuService.getAllSkusImagesById(skuIds));

            vo.setStoreName(baseService.getSpuStoreNameById(vo.getStoreId()));

            vo.setCategoryName(baseService.getSpuCategoryNameById(vo.getCategoryId()));
        }


        return Result.ok(spuVo);
    }

    @PostMapping("/takeOff")
    public Result<Boolean> takeOff(@RequestParam("spuId") Long spuId){

        int re = baseService.updateStatus(0,spuId);

        return Result.ok(re>0);
    }

    @PostMapping("/putOn")
    public Result<Boolean> putOn(@RequestParam("spuId") Long spuId){

        int re = baseService.updateStatus(1,spuId);

        return Result.ok(re>0);
    }

    @PostMapping("/updateStock")
    public Result<Boolean> updateStock(@RequestParam("skuId") Long skuId,@RequestParam("stock") Long stock){

        int re = skuService.updateSkuStock(skuId, stock);

        return Result.ok(re>0);
    }

    @Transactional
    @PostMapping("/addSpu")
    public Result<String> addSpu(@RequestBody SpuVo spuVo){
        AdminSpuExam spu = BeanUtils.copyBean(spuVo, AdminSpuExam.class);

        spu.setPublishStatus(0);

        adminSpuExamService.save(spu);

        Long spuId = spu.getId();

        adminSpuExamService.addSpuDesc(spuId,spuVo.getDesc());

        List<AdminSpuAttrValueExam> attrValueList = BeanUtils.copyList(spuVo.getBaseAttrs(),AdminSpuAttrValueExam.class);

        for (AdminSpuAttrValueExam adminSpuAttrValueExam : attrValueList) {
            adminSpuAttrValueExam.setSpuId(spuId);
            adminSpuAttrValueExam.setSort(1);
        }

        adminSpuAttrValueExamService.saveBatch(attrValueList);

        List<AdminSkuExam> skuExamList = BeanUtils.copyList(spuVo.getSkus(),AdminSkuExam.class);

        int count = 0;

        for (AdminSkuExam adminSkuExam : skuExamList) {
            adminSkuExam.setSpuId(spuId);
            adminSkuExam.setCategoryId(spuVo.getCategoryId());
            adminSkuExam.setCategoryId(spu.getCategoryId());
            adminSkuExam.setStoreName(storeService.getById(adminSkuExam.getStoreId()).getName());

            List<AdminSkuAttrValueExam> skuAttrValueExamList = BeanUtils.copyList(spuVo.getSkus().get(count).getSaleAttrs(),AdminSkuAttrValueExam.class);

            List<String> imgList = BeanUtils.copyList(spuVo.getSkus().get(count).getImages(),String.class);

            StringBuilder subtitle = new StringBuilder();

            for (AdminSkuAttrValueExam skuAttrValueExam : skuAttrValueExamList) {
                subtitle.append(skuAttrValueExam.getAttrValue());
            }
            adminSkuExam.setSubtitle(subtitle.toString());
            adminSkuExam.setDefaultImage(imgList.get(0));

            log.info("skuAttrValueExamList:{}",skuAttrValueExamList);
            log.info("subtitle:{}",subtitle);

            adminSkuExamService.save(adminSkuExam);

            Long skuId = adminSkuExam.getId();

            int sort = 1;

            for (AdminSkuAttrValueExam skuAttrValueExam : skuAttrValueExamList) {
                Long attrId = adminSkuExamService.addAttr(skuAttrValueExam);
                skuAttrValueExam.setSkuId(skuId);
                skuAttrValueExam.setSort(1);
                skuAttrValueExam.setAttrId(attrId);
                sort++;
            }
            adminSkuAttrValueExamService.saveBatch(skuAttrValueExamList);

//            List<String> imgList = BeanUtils.copyList(spuVo.getSkus().get(count).getImages(),String.class);

            int num = 1;

            for (String i : imgList) {
                AdminSkuImagesExam adminSkuImagesExam = new AdminSkuImagesExam();

                adminSkuImagesExam.setUrl(i);
                adminSkuImagesExam.setSort(num);
                adminSkuImagesExam.setSkuId(skuId);

                if(num == 1){
                   adminSkuImagesExam.setDefaultStatus(1);
                }

                adminSkuImagesExamService.save(adminSkuImagesExam);

                num++;
            }

            count++;
        }

        return Result.ok("添加商品成功");
    }

    @GetMapping("/getCategoryList")
    public Result<List<GoodsCategory>> getCategoryList(){

        return Result.ok(goodsCategoryService.getGoodsCategoryList());
    }

}
